package com.ymatou.iapi.hunter.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.hunter.paramater.GetHunterFansListRequest;
import com.ymatou.iapi.hunter.service.GetHunterFansCall;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceHunterOrderCallService;
import com.ymttest.database.model.hunter.HunterFans;
import com.ymttest.database.sqlwapper.hunter.HunterFansWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_GetHunterFans {
	
	private static GetHunterFansListRequest getHunterFansListRequest;
	private static GetHunterFansCall getHunterFansCall;
	HunterFansWrapper wrapper = new HunterFansWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查看推手的粉丝");
	}

	@Before
	public void caseUp() {
		getHunterFansListRequest = new GetHunterFansListRequest();
		getHunterFansCall = new GetHunterFansCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getHunterFansCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb(long hunterId, JSONObject object) throws JSONException {
		
		List<HunterFans> actualResult = wrapper.selectFansByHunterId(hunterId);
		
		Logger.verifyEquals(object.getInt("totalRecords"), actualResult.size(),
				"验证totalRecords");
		
		for(int i = 0; i < actualResult.size(); i++) {

			Logger.verifyEquals(object.getJSONArray("userList")
					.getJSONObject(i).getLong("userId"), actualResult.get(i).getUser_id(),
					"验证userId");
			Logger.verifyEquals(object.getJSONArray("userList")
					.getJSONObject(i).getString("userName"), actualResult.get(i).getUser_name(),
					"验证userName");
			Logger.verifyEquals(0, actualResult.get(i).getStatus(),
					"验证Status");
			DataUtil.verifyDateEquals(object.getJSONArray("userList")
					.getJSONObject(i).getString("addTime"), actualResult.get(i).getAdd_time(),
					"验证addTime");
			
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterFans_001() {
		Logger.start(true, "查看推手的粉丝");
		try {
			
			
			long hunterId = 4321L;
			List<HunterFans> actualResult = wrapper.selectFansByHunterId(hunterId);
			
			getHunterFansListRequest.setHunterId(hunterId);
			getHunterFansListRequest.setFansUserName("");
			getHunterFansListRequest.setPageIndex(1);
			getHunterFansListRequest.setPageSize(actualResult.size());
			
			getHunterFansCall.setData(getHunterFansListRequest);
			getHunterFansCall.callService();
			
			Logger.verifyEquals("success", getHunterFansCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterFansCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterFansCall.getResponseData().getBoolean("success"),
					"验证success");
			
			verifyDb(hunterId, getHunterFansCall.getResult());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterFans_002() {
		Logger.start(true, "根据粉丝用户名查找粉丝");
		try {
			
			// 创建订单
			Logger.comment("前置数据准备");

			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			String fansName = JUserCallService.GetUserInfo(userId).getLoginId();
			long hunterId = 4321;
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 2);
			long tradingId = placeOrder.getTradingId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();

			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");

			PayGateWayCallService.notifyCall("13", thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(), userId);
			TradingSyncService.getSyncResult(orderId);
			Logger.comment("前置数据准备结束");
			
			Thread.sleep(3000);
			
			getHunterFansListRequest.setHunterId(hunterId);
			getHunterFansListRequest.setFansUserName(fansName);
			getHunterFansListRequest.setPageIndex(1);
			getHunterFansListRequest.setPageSize(10);
			
			getHunterFansCall.setData(getHunterFansListRequest);
			getHunterFansCall.callService();
			
			Logger.verifyEquals("success", getHunterFansCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterFansCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterFansCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterFans fans = wrapper.selectFansByHunterIdAndFansName(hunterId, fansName);
			
			Logger.verifyEquals(getHunterFansCall.getResult().getJSONArray("userList")
					.getJSONObject(0).getLong("userId"), fans.getUser_id(),
					"验证userId");
			Logger.verifyEquals(getHunterFansCall.getResult().getJSONArray("userList")
					.getJSONObject(0).getString("userName"), fans.getUser_name(),
					"验证userName");
			Logger.verifyEquals(0, fans.getStatus(),
					"验证Status");
			DataUtil.verifyDateEquals(getHunterFansCall.getResult().getJSONArray("userList")
					.getJSONObject(0).getString("addTime"), fans.getAdd_time(),
					"验证addTime");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterFans_003() {
		Logger.start(true, "hunter为0");
		try {
			
			long hunterId = 0;
			
			getHunterFansListRequest.setHunterId(hunterId);
			getHunterFansListRequest.setFansUserName("");
			getHunterFansListRequest.setPageIndex(1);
			getHunterFansListRequest.setPageSize(10);
			
			getHunterFansCall.setData(getHunterFansListRequest);
			getHunterFansCall.callService();
			
			Logger.verifyEquals("hunterId:推手用户编号不能为空", getHunterFansCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, getHunterFansCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, getHunterFansCall.getResponseData().getBoolean("success"),
					"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterFans_004() {
		Logger.start(true, "pageSize为0");
		try {
			
			long hunterId = 4321;
			
			getHunterFansListRequest.setHunterId(hunterId);
			getHunterFansListRequest.setFansUserName("");
			getHunterFansListRequest.setPageIndex(1);
			getHunterFansListRequest.setPageSize(0);
			
			getHunterFansCall.setData(getHunterFansListRequest);
			getHunterFansCall.callService();
			
			Logger.verifyEquals("pageSize:每页显示的条数不能为空", getHunterFansCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, getHunterFansCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, getHunterFansCall.getResponseData().getBoolean("success"),
					"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterFans_005() {
		Logger.start(true, "pageIndex为0");
		try {
			
			long hunterId = 4321;
			
			getHunterFansListRequest.setHunterId(hunterId);
			getHunterFansListRequest.setFansUserName("");
			getHunterFansListRequest.setPageIndex(0);
			getHunterFansListRequest.setPageSize(10);
			
			getHunterFansCall.setData(getHunterFansListRequest);
			getHunterFansCall.callService();
			
			Logger.verifyEquals("pageIndex:当前页码不能为空", getHunterFansCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, getHunterFansCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, getHunterFansCall.getResponseData().getBoolean("success"),
					"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterFans_006() {
		Logger.start(true, "当购买了大礼包商品后，解除粉丝关系");
		try {
			
			// 创建普通商品订单,建立粉丝关系
			Logger.comment("前置数据准备");

			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			long hunterId = 4321;
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 2);
			long tradingId = placeOrder.getTradingId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();

			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");

			PayGateWayCallService.notifyCall("13", thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(), userId);
			TradingSyncService.getSyncResult(orderId);
			
			//创建大礼包商品，解除粉丝关系
			TradingServiceResp placeOrderForGift = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 1);
			long tradingIdForGift = placeOrderForGift.getTradingId();
			long orderIdForGift = placeOrderForGift.getOrderList().get(0).getOrderId();
			BigDecimal totalPriceForGift = placeOrderForGift.getPayableAmount();

			Long thirdTradingIdForGift = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingIdForGift, "13");

			PayGateWayCallService.notifyCall("13", thirdTradingIdForGift.toString(),
					totalPriceForGift.subtract(BigDecimal.ZERO).toString(), userId);
			TradingSyncService.getSyncResult(orderIdForGift);
			Logger.comment("前置数据准备结束");
			
			Thread.sleep(5000);
			HunterFans actualResult = wrapper.selectFansByHunterIdAndUserId(hunterId, (long)userId);
			
			Logger.verifyEquals(-1, actualResult.getStatus(),
					"验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterFans_007() {
		Logger.start(true, "根据粉丝id查找粉丝");
		try {
			
			// 创建订单
			Logger.comment("前置数据准备");

			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			long hunterId = 4321;
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 2);
			long tradingId = placeOrder.getTradingId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();

			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");

			PayGateWayCallService.notifyCall("13", thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(), userId);
			TradingSyncService.getSyncResult(orderId);
			Logger.comment("前置数据准备结束");
			
			Thread.sleep(3000);
			
			getHunterFansListRequest.setHunterId(hunterId);
			getHunterFansListRequest.setFansUserId((long)userId);
			getHunterFansListRequest.setPageIndex(1);
			getHunterFansListRequest.setPageSize(10);
			
			getHunterFansCall.setData(getHunterFansListRequest);
			getHunterFansCall.callService();
			
			Logger.verifyEquals("success", getHunterFansCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterFansCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterFansCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterFans fans = wrapper.selectFansByHunterIdAndUserId((long)hunterId, (long)userId);
			
			Logger.verifyEquals(getHunterFansCall.getResult().getJSONArray("userList")
					.getJSONObject(0).getLong("userId"), fans.getUser_id(),
					"验证userId");
			Logger.verifyEquals(getHunterFansCall.getResult().getJSONArray("userList")
					.getJSONObject(0).getString("userName"), fans.getUser_name(),
					"验证userName");
			Logger.verifyEquals(0, fans.getStatus(),
					"验证Status");
			DataUtil.verifyDateEquals(getHunterFansCall.getResult().getJSONArray("userList")
					.getJSONObject(0).getString("addTime"), fans.getAdd_time(),
					"验证addTime");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterFans_008() {
		Logger.start(true, "根据错误的粉丝id查找粉丝");
		try {
			
			getHunterFansListRequest.setHunterId(4321);
			getHunterFansListRequest.setFansUserId(1L);
			getHunterFansListRequest.setPageIndex(1);
			getHunterFansListRequest.setPageSize(10);
			
			getHunterFansCall.setData(getHunterFansListRequest);
			getHunterFansCall.callService();
			
			Logger.verifyEquals("success", getHunterFansCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterFansCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterFansCall.getResponseData().getBoolean("success"),
					"验证success");
			
			Logger.verifyEquals(
					getHunterFansCall.getResult().getJSONArray("userList")
							.length(), 0, "验证size");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterFans_009() {
		Logger.start(true, "根据粉丝id和错误的粉丝名称查找粉丝");
		try {
			
			getHunterFansListRequest.setHunterId(4321);
			getHunterFansListRequest.setFansUserId(25198657L);
			getHunterFansListRequest.setFansUserName("java");
			getHunterFansListRequest.setPageIndex(1);
			getHunterFansListRequest.setPageSize(10);
			
			getHunterFansCall.setData(getHunterFansListRequest);
			getHunterFansCall.callService();
			
			Logger.verifyEquals("success", getHunterFansCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterFansCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterFansCall.getResponseData().getBoolean("success"),
					"验证success");
			
			Logger.verifyEquals(
					getHunterFansCall.getResult().getJSONArray("userList")
							.length(), 0, "验证size");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
